package calculate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.umpay.util.TimeUtil;

/**
 * <pre>******************  类说明  *********************
 * class        :  DateTimeUtil
 * author       :  wangxu
 * date         :  2014-5-26 
 * description  :  日期时间处理工具类
 * see          :                      
 * ***********************************************</pre>
 */
public class DateTimeUtil {
	/**
	 * 计算两个时间之间的long值差异
	 * <br>format可以是yyyyMMddHHmmss、HHmmss
	 * @throws ParseException 
	 */
	public static long minusDate(String time1, String time2, String format) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		long l1 = sdf.parse(time1).getTime();
		long l2 = sdf.parse(time2).getTime();

		return (l2 - l1) / 1000;
	}
	/**
	 * 获取当前日期的前(或)offer天的日期
	 */
	public static String getOfferDay(String platDate,String format,int offer) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(platDate));
		cal.add(Calendar.DAY_OF_YEAR, offer);
		
		return sdf.format(cal.getTime());
	}
	/**
	 * 获取当前日期的前(或)offer月的日期
	 */
	public static String getOfferMonth(String platDate,String format,int offer) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(platDate));
		cal.add(Calendar.MONTH, offer);
		
		return sdf.format(cal.getTime());
	}
	/**
	 * 根据platdate转成yyMM
	 * @param platDate
	 * @return
	 * @throws ParseException
	 */
	public static String transForYYMM(String platDate) throws ParseException{
		if (null == platDate || platDate.trim().length() == 0)
			return "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyMM");
		return (sdf2.format(sdf.parse(platDate)));
	}
	/**
	 * 根据platdate转成yyMM
	 * @param platDate
	 * @return
	 * @throws ParseException
	 */
	public static String transForYYYYMM(String platDate) throws ParseException{
		if(null==platDate||platDate.trim().length()==0)
			return "";
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMM");
			return (sdf2.format(sdf.parse(platDate)));
	}
	/**
	 * 获得当前14位日期
	 * @return
	 * @throws ParseException
	 */
	public static String time14() {
		return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	}
	
	public static String date8() {
		return new SimpleDateFormat("yyyyMMdd").format(new Date());
	}
	
	public static String time6() {
		return new SimpleDateFormat("HHmmss").format(new Date());
	}
	
	/**
	 * 校验输入参数的日期是否在前后两天内
	 * 输入8位日期
	 * @return
	 * @throws ParseException 
	 * @throws ParseException
	 */
	public static boolean timeBetween2Days(String platDate) throws ParseException{
		String beforeDay = getOfferDay(TimeUtil.date8(), "yyyyMMdd", -2);
		String afterDay = getOfferDay(TimeUtil.date8(), "yyyyMMdd", 2);
		if(platDate.compareTo(afterDay)<=0&&platDate.compareTo(beforeDay)>=0){
			return true;
		}
		return false;
	}
	/**
	 * 校验输入参数的日期是否在前后两天内
	 * 输入8位日期
	 * @return
	 * @throws ParseException 
	 * @throws ParseException
	 */
	public static boolean timeBetween1Days(String reqDate) throws ParseException{
		String beforeDay = getOfferDay(TimeUtil.date8(), "yyyyMMdd", -1);
		String afterDay = getOfferDay(TimeUtil.date8(), "yyyyMMdd", 1);
		if(reqDate.compareTo(afterDay)<=0&&reqDate.compareTo(beforeDay)>=0){
			return true;
		}
		return false;
	}
	
	/**
	 * 返回系统当前时间，时间格式与传入的模板一致
	 * @param model
	 * @return
	 */
	public static String getTime(String model){
		String time = null;
		SimpleDateFormat df = new SimpleDateFormat(model);//设置日期格式
		time = df.format(new Date());
		return time;
	}
	
	public static void main(String[] args) {
		try {
//			System.out.println(getOfferMonth("20140801","yyyyMMdd",-2));
//			System.out.println(getOfferDay("20140909", "yyyyMMdd", -2));
//			System.out.println(timeBetween2Days("43332424"));
			System.out.println(getTime("HH:mm:ss"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}



